<%@page import="java.util.UUID"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/pages/common/taglib.jsp" %>
<%
    String uuid = UUID.randomUUID().toString();
    request.setAttribute("uuid", uuid);
%>
   
    <div id="${uuid}_userExtTab">
    	<ul class="nav nav-tabs" role="tablist">
    		<li role="presentation" class="active">
    			<a href="#userInfoBox" role="tab" data-toggle="tab">
    			<span class="glyphicon glyphicon-user" aria-hidden="true"></span> 用户信息
    			</a>
    		</li>
    		<li role="presentation">
    			<a href="#userOrgTab_${uuid}" role="tab" data-toggle="tab">
    			<span class="glyphicon glyphicon-home" aria-hidden="true"></span> 所属机构
    			</a>
    		</li>
    		<li role="presentation">
    			<a href="#userRoleTab_${uuid}" role="tab" data-toggle="tab">
    			<span class="glyphicon glyphicon-check" aria-hidden="true"></span> 角色配置
    			</a>
    		</li>
    	</ul>
    	<div class="tab-content">
    		<div role="tabpanel" class="tab-pane fade in active"  id="userInfoBox">
		        <form  id="userForm" class="container-fluid am-margin-top">
		            <input type="hidden" name="id"/>
		            <input type="hidden" name="orgId"/>
		            
		            <div class="row">
		                <div class="form-group col-sm-6">
		                    <label for="loginName">登录名</label>
		                    <input type="text" name="loginName" class="form-control" check-type="required">
		                </div>
		                <div class="form-group col-sm-6">
		                    <label for="password">密码</label>
		                    <input type="password" name="password" class="form-control" check-type="pwdStrength,required">
		                	
		                </div>
					</div>
					
					<div class="row">
		                <div class="form-group col-sm-6">
		                    <label for="userName">名称</label>
		                    <input type="text" name="userName" class="form-control" check-type="required">
		                </div>
		                <div class="form-group col-sm-6">
		                    <label for="mobileNo">手机号码</label>
		                    <input type="text" name="mobileNo"  class="form-control">
		                </div>
					</div>
					
					<div class="row">
		                <div class="form-group col-sm-6">
		                    <label for="idCardNo">证件号码</label>
		                    <input type="text" name="idCardNo" class="form-control" >
		                </div>
		                <div class="form-group col-sm-6">
		                        是否被锁:&nbsp;<input type="checkbox" name="locked" value="1" >
		                        &nbsp;&nbsp;
		                        是否有效:&nbsp;<input type="checkbox" name="enabled" value="1" checked="checked">
		                </div>
	                </div>
	                
	                <div class="row">
		                <div class="form-group col-sm-6">
		                    <label for="userName">用户类型</label>
		                    <select class="form-control" name="userType">
		                    	<pf:dataDic var="item" dicCode="USER_TYPE">
	                                <option value="${item.value}">${item.name}</option>
	                            </pf:dataDic>
		                    </select>
		                </div>
					</div>
	
	                <div class="form-group">
	                    <a href="javascript:void(0)" id="userSaveBt"  class="btn btn-primary">保存</a>
	                </div>
		        </form>
		
		    </div>
    	
    		<div role="tabpanel" class="tab-pane fade"  id="userOrgTab_${uuid}">
    			<div id="userOrgGridBox_${uuid}" style="display: none;" >
    				<div class="row">
    					<div class="col-md-12"  >
    						<div>
    							<div class="box-header with-border">
    								<h3 class="box-title"></h3>
    								<div class="box-tools pull-right" id="userOrgGridTb_${uuid}">
    									<shiro:hasPermission name="USER_ORG_REL_ADD">
							    		<button class="btn btn-sm btn-success  addBtn">
							    			<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 追加
							    		</button>
							    		</shiro:hasPermission>
							    		
							    		<shiro:hasPermission name="USER_ORG_REL_DEL">
							    		<button class="btn btn-sm btn-danger delBtn">
							    			<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 移除
							    		</button>
							    		</shiro:hasPermission>
    								</div>
    							</div>
    							<div class="box-body">
    								<table id="userOrgGrid_${uuid}" style="width: 100%"></table>
    							</div>
    						</div>
    					</div>
    				</div>
    			</div>
    		</div>
    		<div role="tabpanel" class="tab-pane fade"  id="userRoleTab_${uuid}">
    			<div id="userRoleGridBox_${uuid}" style="display: none;">
    				<div class="row">
    					<div class="col-md-12"  >
    						<div>
    							<div class="box-header with-border">
    								<h3 class="box-title"></h3>
    								<div class="box-tools pull-right" id="userRoleGridTb_${uuid}">
    									<shiro:hasPermission name="USER_ROLE_REL_ADD">
							    		<button class="btn btn-sm btn-success  addBtn">
							    			<span class="glyphicon glyphicon-plus" aria-hidden="true"></span> 追加
							    		</button>
							    		</shiro:hasPermission>
							    		
							    		<shiro:hasPermission name="USER_ROLE_REL_DEL">
							    		<button class="btn btn-sm btn-danger delBtn">
							    			<span class="glyphicon glyphicon-trash" aria-hidden="true"></span> 移除
							    		</button>
							    		</shiro:hasPermission>
    								</div>
    							</div>
    							<div class="box-body">
    								<table id="userRoleGrid_${uuid}" style="width: 100%"></table>
    							</div>
    						</div>
    					</div>
    				</div>
    			</div>
    		</div>
    	</div>
    </div>
<script type="text/javascript">
    $(function () {
        var uuid = '${uuid}';
        var id = '${param.id}';
        var orgId = '${param.orgId}';
        var userForm = $("#userForm");
        
        if (id) {
            $.getJSON(ctx + '/userAction/getUserById.do', {id: id}, function (data) {
                data.password = null;
                webUtils.fullForm({
                    form: $("#userForm"),
                    data: data
                });
            });
            buildUserRole(id);
            buildUserOrg(id);
            
            userForm.find("input[name=password]").attr("check-type","pwdStrength").
            parent().append('<p class="help-block">不填写则表示不修改密码</p>');
            
        }else{
        	$("#userInfoBox").removeClass("col-md-5");
        }
        
        userForm.validation();
        
        if (orgId) {
            userForm.find("input[name=orgId]").val(orgId);
        }
        
        

        $("#userSaveBt").click(function () {
            if(userForm.valid()){
            	var datas=webUtils.formToObj(userForm);
                var postDataStr=JSON.stringify(datas);
    			var postDataStrBase64=Base64.encode(postDataStr);
    			$.post(ctx + '/userAction/saveUser.do',{
    				code:postDataStrBase64,
    				orgId:userForm.find("input[name=orgId]").val()
    			},function(data){
    				if (data.success) {
                        webUtils.alert("提交成功");
                        top.webUtils.topicPublish("user.edit.success", data);
                    } else {
                    	webUtils.error(data.msg);
                    }
    			},"json");
            }
        });

        function buildUserRole(userId) {
            var userRoleGrid = $("#userRoleGrid_"+uuid);
            $("#userRoleGridBox_"+uuid).show();

            userRoleGrid = userRoleGrid.bootstrapTable({
            	url:ctx + "/userAction/queryUserRoles.do?userId=" + userId,
                columns: [
						{field: '_id',checkbox:true},
                        {title: '角色名称', field: 'roleName', width: 120},
                        {title: '标识', field: 'roleKey',  width: 120}
                    ]
            });
            
            var _userRoleGridTb=$("#userRoleGridTb_"+uuid);
            _userRoleGridTb.find(".addBtn").click(function(){
            	
            	webUtils.roleSelector({
            		onSelect:function(rows){
            			if(rows && rows.length>0){
            				var ids=[];
            				$.each(rows,function(i,n){
            					ids.push(n.id);
            				});
            				$.post(ctx + "/userAction/saveUserRole.do", {userId: userId, roleIds: ids.join(",")}, function (data) {
                                if (data.success) {
                                    userRoleGrid.bootstrapTable("refresh");
                                } else {
                                    webUtils.alert(data.msg);
                                }
                            }, "json");
            			}
            		}
            	});
            });
            
            _userRoleGridTb.find(".delBtn").click(function(){
            	webUtils.confirm("删除后将不可恢复，请问是否继续?", function(r) {
            		if(r){
            			var rows = userRoleGrid.bootstrapTable("getSelections");
                        if (!rows || rows.length <= 0) {
                            webUtils.alert("请选择需要移除的数据");
                        } else {
                            var selecteds = [];
                            $.each(rows, function (i, n) {
                                selecteds.push(n.id);
                            });
                            $.post(ctx + "/userAction/removeUserRole.do", {
                                userId: userId,
                                roleIds: selecteds.join(",")
                            }, function (data) {
                                if (data.success) {
                                    userRoleGrid.bootstrapTable("refresh");
                                } else {
                                    webUtils.error(data.msg);
                                }
                            }, 'json');
                        }
            		}
            	});
            });
        };

        function buildUserOrg(userId) {
            var userOrgGrid = $("#userOrgGrid_"+uuid);
            $("#userOrgGridBox_"+uuid).show();
            userOrgGrid = userOrgGrid.bootstrapTable({
            	url: ctx + "/userAction/queryUserOrgs.do?userId=" + userId,
                columns: [
						{field: '_id',checkbox:true},
                        {title: '部门名称', field: 'name', align: 'left', width: 220},
                        {title: '全称', field: 'fullName', align: 'left', width: 220}
                    ]
            });
            
            var _userOrgGridTb= $("#userOrgGridTb_"+uuid);
            _userOrgGridTb.find(".addBtn").click(function(){
            	webUtils.orgSelector({
                    scope: this,
                    onSelect: function (rows) {
                        var orgIds=[]
                        $.each(rows,function(i,n){
                            orgIds.push(n.id)
                        });
                        $.post(ctx + "/userAction/saveUserOrg.do", {userId: userId,orgIds: orgIds.join(",")}, function (data) {
                            if (data.success) {
                                userOrgGrid.bootstrapTable("refresh");
                            } else {
                            	webUtils.error(data.msg);
                            }
                        }, "json");
                        
                    }
                });
            });
            
            
            _userOrgGridTb.find(".delBtn").click(function(){
            	webUtils.confirm("删除后将不可恢复，请问是否继续?", function(r) {
            		if(r){
            			var rows = userOrgGrid.bootstrapTable("getSelections");
                        if (!rows || rows.length <= 0) {
                            webUtils.alert("请选择需要移除的数据");
                        } else {
                            var selecteds = [];
                            $.each(rows, function (i, n) {
                                selecteds.push(n.id);
                            });
                            $.post(ctx + "/userAction/removeUserOrg.do", {
                                userId: userId,
                                orgIds: selecteds.join(",")
                            }, function (data) {
                                if (data.success) {
                                	userOrgGrid.bootstrapTable("refresh");
                                } else {
                                    webUtils.error(data.msg);
                                }
                            }, 'json');
                        }
            		}
            	});
            });
        };
    });
</script>